<?php
namespace app\common\controller;
use think\Request;
/**
 * 基础控制器
 */
class CommonController extends \think\Controller
{
    /**
     * 请求对象
     */
    protected  $request;

    /**
     * 跨域处理
     */
    public function __construct()
    {
        $this->request = Request::instance();
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: token, Origin, X-Requested-With, Content-Type, Accept, Authorization");
        header('Access-Control-Allow-Methods: POST,GET,PUT,DELETE,OPTIONS,PATCH');
        header('Access-Control-Max-Age: 3600');
    }

     /**
     * 统一返回json方法
     * code 1 成功 失败代码具体定义
     */
    protected function jsonJump($code = 1 , $msg = '成功' , $data = [])
    {
        if (!is_numeric($code)) {
            echo json_encode(['code' => -1,'msg' => 'code值必须为数字'],JSON_UNESCAPED_UNICODE);exit;
        }
        $result = [
            "code"  => $code,
            "msg"   => $msg,
            "data"  => $data
        ];
        echo json_encode($result,JSON_UNESCAPED_UNICODE);exit;
    }

}